package cn.com.ddapp.ddm.ui;

import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoLinearLayout;

import org.json.JSONObject;

import butterknife.BindView;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import okhttp3.Call;

public class OpinionActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.right_text)
    TextView right_text;
    @BindView(R.id.it_text)
    EditText it_text;
    @BindView(R.id.tv_submit)
    TextView tv_submit;
    private String coment;
    int orientation;

    @Override
    public void initView() {
        titletext.setText("意见反馈");
        right_text.setVisibility(View.VISIBLE);
        right_text.setText("清除");

        /*DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        ViewGroup.LayoutParams layoutParams = tv_submit.getLayoutParams();

        orientation = getResources().getConfiguration().orientation;
        if (orientation == 0 || orientation == 2) {
            //layoutParams.height = (int) (screenHeigh * 0.5); // 高度设置为屏幕的0.6;
            layoutParams.width = (int) (screenWidth * 0.5);// 宽度设置为屏幕的0.4
            tv_submit.setLayoutParams(layoutParams);
        }*/

        back.setOnClickListener(this);
        right_text.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_opinion;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.right_text:
                it_text.setText("");
                it_text.setHint("说点你的心得吧");
                it_text.clearFocus();
                break;
            case R.id.tv_submit:
                coment = it_text.getText().toString().trim();
                if(TextUtils.isEmpty(coment)){
                    BaseApplication.showToast("内容不能为空");
                }else{
                    httpData();
                }
                break;
        }
        super.onClick(view);
    }

    private void httpData() {
        OkUtilCallback callback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                try{
                    JSONObject jsonObject=new JSONObject(response);
                    if("OK".equals(jsonObject.getString("result"))){
                        BaseApplication.showToast("反馈成功");
                        finish();
                    }
                }
                catch (Exception e){
                    BaseApplication.showToast("反馈失败");
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }
        };
        String userId = SPUtils.getStringData(this, SPUtils.USERID);
        OKUtilAPI.post(OKUtilAPI.getFeeds, "userId=" + userId+"&content="+coment, callback);
    }
}
